﻿Imports System.Data.OleDb
Public Class frmThemGia
#Region "Form Event"
    Public IDSanPham As Integer = -1
    Private Sub frmThemSanPham_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If IDSanPham <> -1 Then
                Dim dbConn As New OleDb.OleDbConnection
                Dim dataAccess As New DataAccess
                Dim dsSanPham As New DataSet
                Dim dtSanPham As New DataTable
                Dim query As String = "Select * From SanPham Left Outer Join LoaiSanPham On SanPham.IdLoai=LoaiSanPham.IdLoai Where IDSanPham=" & IDSanPham
                Dim adapter As OleDb.OleDbDataAdapter
                dataAccess.ConnectDatabase_DBAccess(dbConn)
                adapter = New OleDb.OleDbDataAdapter(query, dbConn)
                adapter.Fill(dsSanPham)
                dbConn.Close()
                dtSanPham = dsSanPham.Tables(0)
                txtLoaiSanPham.Text = dtSanPham.Rows(0)("TenLoai")
                txtMaLoai.Text = dtSanPham.Rows(0)("MaSanPham")
                txtSanPham.Text = dtSanPham.Rows(0)("TenSanPham")
                txtGhiChu.Text = IIf(IsDBNull(dtSanPham.Rows(0)("GhiChu")), "", dtSanPham.Rows(0)("GhiChu"))
                'txtDonViTinh.Text = dtSanPham.Rows(0)("TenDonVi")
                'txtQuyCach.Text = dtSanPham.Rows(0)("TenQuyCach")
                txtDonGia.Text = dtSanPham.Rows(0)("DonGia")
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Button Event"
    'disable text

    

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            If txtDonGia.Text = "" Then
                EmptyData(txtMaLoai)
                Exit Sub
            End If
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            Dim dbCommand As OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim query As String = ""
            query = String.Format("Update SanPham Set DonGia={0},GhiChu=N'{1}' Where IDSanPham={2}", Double.Parse(txtDonGia.Text, Globalization.NumberStyles.Currency), txtGhiChu.Text.Trim, IDSanPham)
            dbCommand = New OleDbCommand(query, dbConn)
            dbCommand.ExecuteNonQuery()
            dbConn.Close()
            txtMaLoai.Text = ""
            txtSanPham.Text = ""
            txtDonGia.Text = "0.00"
            frmBangGia.SanPham_Load()
            QuanLyKhachHang.frmHopDong.AutoComplete()
            Me.Close()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Try
            txtMaLoai.Text = ""
            txtSanPham.Text = ""
            txtDonGia.Text = "0.00"
            frmBangGia.SanPham_Load()
            Me.Close()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
    Private Function IsDotted(ByVal KeyChar As Char) As Boolean
        Try
            If KeyChar = "."c Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception

        End Try
    End Function
    Private Function IsOnlyDotted(ByVal TextInput As String) As Boolean
        Try
            Dim aTextInput As String()
            aTextInput = TextInput.Split("."c)
            If aTextInput.Length > 2 Then
                Return False
            Else
                Return True
            End If
        Catch ex As Exception

        End Try
    End Function
    Private Sub txtDonGia_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtDonGia_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
        If txtDonGia.Text <> "" Then
            txtDonGia.Text = txtDonGia.Text.Replace(",", "").Replace(".", "")
            txtDonGia.Text = CDec(txtDonGia.Text).ToString("N", Globalization.CultureInfo.CreateSpecificCulture("vi-vn"))
        End If
    End Sub

    Private Sub txtLoaiSanPham_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtLoaiSanPham.KeyPress
        Try
            e.Handled = True
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtMaLoai_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMaLoai.KeyPress
        Try
            e.Handled = True
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtSanPham_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSanPham.KeyPress
        Try
            e.Handled = True
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtQuyCach_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtQuyCach.KeyPress
        Try
            e.Handled = True
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtDonViTinh_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDonViTinh.KeyPress
        Try
            e.Handled = True
        Catch ex As Exception

        End Try
    End Sub
End Class